package com.vdg.ui.fragment;

import java.util.ArrayList;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.vdg.R;
import com.vdg.dialog.MenuDialog;
import com.vdg.dialog.MenuDialog.OnDialogSelectedItemListener;
import com.vdg.fps.FetchFriendsListActivity;
import com.vdg.fps.FetchGroupsListActivity;
import com.vdg.fps.FetchLikedPagesListActivity;
import com.vdg.fps.FetchMyPagesActivity;
import com.vdg.fps.LoginActivity;
import com.vdg.fps.LoginSettingActivity;
import com.vdg.fps.PostMessageActivity;
import com.vdg.fps.SendPrivateMessageActivity;
import com.vdg.fps.adapter.ListFaceBookAccountsAdapter;
import com.vdg.fps.data.FaceBookAccount;
import com.vdg.fps.listener.OnQueryListAccountListener;
import com.vdg.fps.listener.OnRequestListnener;
import com.vdg.fps.services.FPSService;
import com.vdg.fps.services.FPSService.LocalBinder;

public class AccountsFragment extends Fragment {
	private FPSService mService;
	private View v;
	private RelativeLayout mBtnAddAccounts;
	private static final String TAG = "AccountsFragment";
	private ListFaceBookAccountsAdapter mListFaceBookAccountsAdapter;
	private ArrayList<FaceBookAccount> mListFaceBookAccounts;
	private ListView mListView;
	private MenuDialog mMenuDialog;
	private int mItemSelected;
	public static String FACE_BOOK_ACCOUNT_KEY = "facebook_account";
	private OnDialogSelectedItemListener mOnDialogSelectedItemListener = new OnDialogSelectedItemListener() {

		@Override
		public void onSelectedFetchGroupsList() {
			// TODO Auto-generated method stub
			Intent intent = new Intent(getActivity(),
					FetchGroupsListActivity.class);
			intent.putExtra(FACE_BOOK_ACCOUNT_KEY, mItemSelected);
			startActivity(intent);
		}

		@Override
		public void onSelectedFetchFriendsList() {
			// TODO Auto-generated method stub
			Intent intent = new Intent(getActivity(),
					FetchFriendsListActivity.class);
			intent.putExtra(FACE_BOOK_ACCOUNT_KEY, mItemSelected);
			startActivity(intent);
		}

		@Override
		public void onSelectedFetchLikedPages() {
			// TODO Auto-generated method stub
			Intent intent = new Intent(getActivity(),
					FetchLikedPagesListActivity.class);
			intent.putExtra(FACE_BOOK_ACCOUNT_KEY, mItemSelected);
			startActivity(intent);
		}

		@Override
		public void onSelectedYourPages() {
			// TODO Auto-generated method stub
			Intent intent = new Intent(getActivity(),
					FetchMyPagesActivity.class);
			intent.putExtra(FACE_BOOK_ACCOUNT_KEY, mItemSelected);
			startActivity(intent);

		}

		@Override
		public void onSelectedPostMessage() {
			// TODO Auto-generated method stub
			Intent intent = new Intent(getActivity(), PostMessageActivity.class);
			intent.putExtra(FACE_BOOK_ACCOUNT_KEY, mItemSelected);
			startActivity(intent);
		}

		@Override
		public void onSelectedSendPrivate() {
			// TODO Auto-generated method stub
			Intent intent = new Intent(getActivity(),
					SendPrivateMessageActivity.class);
			intent.putExtra(FACE_BOOK_ACCOUNT_KEY, mItemSelected);
			startActivity(intent);
		}
	};
	private ServiceConnection mServiceConnection = new ServiceConnection() {
		@TargetApi(Build.VERSION_CODES.HONEYCOMB)
		// required for Sherlock's invalidateOptionsMenu */
		public void onServiceConnected(ComponentName name, IBinder service) {
			LocalBinder binder = (LocalBinder) service;
			Log.v(TAG, "service connected");
			mService = binder.getService();
			init();
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			// TODO Auto-generated method stub

		}
	};

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		if (mService != null) {
			mService.queryListAccount(new OnQueryListAccountListener() {

				@Override
				public void onResponse(ArrayList<FaceBookAccount> listAccount) {
					// TODO Auto-generated method stub
					mListFaceBookAccounts.clear();
					mListFaceBookAccounts.addAll(listAccount);
					mListFaceBookAccountsAdapter.notifyDataSetChanged();
				}
			}, getActivity());
		}
		super.onResume();
	}

	public View onCreateView(android.view.LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {
		v = inflater.inflate(R.layout.accounts_layout, container, false);
		Intent serviceIntent = new Intent(getActivity(), FPSService.class);
		getActivity().bindService(serviceIntent, mServiceConnection,
				Activity.BIND_AUTO_CREATE);
		return v;
	};

	private void init() {
		mListFaceBookAccounts = new ArrayList<FaceBookAccount>();
		mBtnAddAccounts = (RelativeLayout) v.findViewById(R.id.btn_add);
		mListView = (ListView) v.findViewById(R.id.liv_accounts);
		mBtnAddAccounts.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(),
						LoginSettingActivity.class);
				startActivity(intent);
			}
		});
		mListFaceBookAccountsAdapter = new ListFaceBookAccountsAdapter(
				getActivity(), 0, mListFaceBookAccounts);
		mListView.setAdapter(mListFaceBookAccountsAdapter);
		mService.queryListAccount(new OnQueryListAccountListener() {
			@Override
			public void onResponse(ArrayList<FaceBookAccount> listAccount) {
				mListFaceBookAccounts.clear();
				mListFaceBookAccounts.addAll(listAccount);
				mListFaceBookAccountsAdapter.notifyDataSetChanged();
				// TODO Auto-generated method stub

			}
		}, getActivity());
		mMenuDialog = new MenuDialog(getActivity(),
				mOnDialogSelectedItemListener);
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mMenuDialog.showAtCenter(view);
				mItemSelected = position;
				// TODO Auto-generated method stub
			}
		});
		registerForContextMenu(mListView);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		getActivity().unbindService(mServiceConnection);
		super.onDestroy();
	}
}
